package com.example.arithmeticleetcode.leetcode;

/**
 * @program: arithmetic-leetcode
 * @description: 1413. 逐步求和得到正数的最小值
 * @author: FangZhen
 * @create: 2020-05-09 15:40
 **/
public class Demo15 {

    public static void main(String[] args) {
        int[] nums1 = {-3, 2, -3, 4, 2}; // 5
        int[] nums2 = {1, 2}; // 1
        int[] nums3 = {1, -2, -3}; // 5
        int[] nums4 = {2, 3, 5, -5, -1}; // 1
        System.out.println(minStartValue(nums1));
        System.out.println(minStartValue(nums2));
        System.out.println(minStartValue(nums3));
        System.out.println(minStartValue(nums4));
    }

    public static int minStartValue(int[] nums) {
        int res = Integer.MAX_VALUE;
        int sum = 0;
        for (int val : nums) {
            sum += val;
            res = Math.min(res, sum);
        }
        return res < 0 ? Math.abs(res) + 1 : 1;
    }
}
